package org.yelongframework.model.generator.freemarker.datamodel;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.yelongframework.commons.lang.ClassUtil;
import org.yelongframework.model.generator.manager.GenFieldAndColumn;
import org.yelongframework.model.generator.manager.GenModelAndTable;
import org.yelongframework.model.generator.manager.GenModelAndTableProperties;

/**
 * 默认模板模型工厂实现
 * 
 * @since 1.0.0
 */
public class DefaultModelGenModelDataModelBuilder implements ModelGenModelDataModelBuilder {

	// ==================================================构建==================================================

	@Override
	public ModelGenModelDataModel build(GenModelAndTable modelAndTable) {
		// 模型表
		ModelGenModelDataModel templateModel = new ModelGenModelDataModel(modelAndTable);
		String modelName = modelAndTable.getModelClassSimpleName();
		templateModel.setModelName(modelName);
		templateModel.setModelPackage(modelAndTable.getModelClassPackageName());
		templateModel.setTableName(modelAndTable.getTableName());
		templateModel.setModelNamePrefixLowerCase(ClassUtil.getSimpleNamePrefixLowerCase(modelName));
		templateModel.setTableDesc(modelAndTable.getTableDesc());
		GenModelAndTableProperties genModelAndTableProperties = modelAndTable.getProperties();
		templateModel.setAuthor(genModelAndTableProperties.getAuthor());
		templateModel.setSuperClassName(genModelAndTableProperties.getSuperClassName());
		templateModel.setSuperClassSimpleName(genModelAndTableProperties.getSuperClassSimpleName());
		// 字段列
		List<GenFieldAndColumn> fieldAndColumns = modelAndTable.getGenFieldAndColumnAll();
		List<ModelGenModelFieldDataModel> modelFields = new ArrayList<ModelGenModelFieldDataModel>(
				fieldAndColumns.size());
		for (GenFieldAndColumn fieldAndColumn : fieldAndColumns) {
			if (null == fieldAndColumn) {
				continue;
			}
			ModelGenModelFieldDataModel templateModelField = new ModelGenModelFieldDataModel(fieldAndColumn);
			String fieldName = fieldAndColumn.getFieldName();
			templateModelField.setCode(fieldName);
			templateModelField.setCodePrefixUpperCase(fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
			templateModelField.setLength(fieldAndColumn.getMaxLength() + "");
			templateModelField.setMandatory(Boolean.toString(fieldAndColumn.isAllowNull()));
			templateModelField.setName(fieldAndColumn.getDesc());
			templateModelField.setPrimaryKey(Boolean.toString(fieldAndColumn.isPrimaryKey()));
			templateModelField.setType(fieldAndColumn.getFieldType().getSimpleName());
			templateModelField.setColumnName(fieldAndColumn.getColumnName());
			StringBuilder columnAnnotation = new StringBuilder("@Column(");
			columnAnnotation.append("column = \"" + fieldName + "\"");
			Long maxLength = fieldAndColumn.getMaxLength();
			if (null != maxLength && fieldAndColumn.getMaxLength() < Long.MAX_VALUE) {
				columnAnnotation.append(", maxLength = " + fieldAndColumn.getMaxLength());
			}
			if (!fieldAndColumn.isAllowNull()) {
				columnAnnotation.append(", allowNull = " + fieldAndColumn.isAllowNull());
			}
			// columnAnnotation.append(",jdbcType = \""+fieldAndColumn.getJdbcType()+"\"");
			String columnName = fieldAndColumn.getColumnName();
			if (StringUtils.isNotBlank(columnName)) {
				columnAnnotation.append(", columnName = \"" + fieldAndColumn.getColumnName() + "\"");
			}
			String desc = fieldAndColumn.getDesc();
			if (StringUtils.isNotBlank(desc)) {
				columnAnnotation.append(", desc = \"" + fieldAndColumn.getDesc() + "\"");
			}
			columnAnnotation.append(")");
			templateModelField.setColumnAnnotation(columnAnnotation.toString());

			modelFields.add(templateModelField);
		}
		templateModel.setModelFields(modelFields);
		return templateModel;
	}

}
